package com.hngy.xpq.android_practice.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.hngy.xpq.android_practice.utils.UserOrder;
import com.hngy.xpq.android_practice.bean.User;

public class SQLiteUser extends SQLiteOpenHelper {          //<用户>
    private SQLiteDatabase sqLiteDatabase;

    public SQLiteUser(Context context) {
        super(context, UserOrder.DATABASE_DATA , null , UserOrder.DATABASE_INS);
        sqLiteDatabase = this.getWritableDatabase();
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + UserOrder.DATABASE_TABLE + "("
                + UserOrder.NOTEPAD_ID + " integer primary key autoincrement,"
                + UserOrder.NOTEPAD_NAME + " VARCHAR(50),"
                + UserOrder.NOTEPAD_KEY + " VARCHAR(50),"
                + UserOrder.NOTEPAD_PHONE + " VARCHAR(20),"
                + UserOrder.NOTEPAD_BORN + " VARCHAR(20),"
                + UserOrder.NOTEPAD_TIME + " VARCHAR(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}

    public boolean addData(User u){
        ContentValues values = new ContentValues();
        values.put(UserOrder.NOTEPAD_NAME , u.getName());
        values.put(UserOrder.NOTEPAD_KEY , u.getKey());
        values.put(UserOrder.NOTEPAD_PHONE , u.getPhone());
        values.put(UserOrder.NOTEPAD_BORN , u.getBorn());
        values.put(UserOrder.NOTEPAD_TIME , UserOrder.getTimeDate());
        return sqLiteDatabase.insert(UserOrder.DATABASE_TABLE , null , values)>0;
    }

    public User dl_query(String name , String key){
        User u = null;
        try {
            Cursor cursor = sqLiteDatabase.rawQuery("select * from " + UserOrder.DATABASE_TABLE +
                    " where " + UserOrder.NOTEPAD_NAME + "=" + name +
                    " and " + UserOrder.NOTEPAD_KEY + "=" + key,null,null );
            if (cursor.moveToNext()){
                u = new User();
                u.setName(cursor.getString(cursor.getColumnIndex(UserOrder.NOTEPAD_NAME)));
                u.setKey(cursor.getString(cursor.getColumnIndex(UserOrder.NOTEPAD_KEY)));
                u.setPhone(cursor.getString(cursor.getColumnIndex(UserOrder.NOTEPAD_PHONE)));
                u.setTime(cursor.getString(cursor.getColumnIndex(UserOrder.NOTEPAD_TIME)));
            } else {
                System.out.println("连接异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return u;
    }

    public User queryNameAndPhone(String name,String phone){
        User u = null;
        try {
            Cursor cursor = sqLiteDatabase.rawQuery("select * from " + UserOrder.DATABASE_TABLE +
                    " where " + UserOrder.NOTEPAD_NAME + "=" + name +
                    " and " + UserOrder.NOTEPAD_PHONE + "=" + phone,null,null );
            if (cursor.moveToNext()){
                u = new User();
                u.setKey(cursor.getString(cursor.getColumnIndex(UserOrder.NOTEPAD_KEY)));
            } else {
                System.out.println("忘记密码中信息错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return u;
    }

    public boolean updateModKey(String name,String key){
        ContentValues values = new ContentValues();
        values.put(UserOrder.NOTEPAD_KEY,key);
        String sql = UserOrder.NOTEPAD_NAME + "=?";
        String[] strings = new String[]{name};
        return
                sqLiteDatabase.update(UserOrder.DATABASE_TABLE,values,sql,strings)>0;
    }

}
